package com.example.demo.config;//package com.example.demo.config;

import com.example.demo.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

/**
 * 处理Springboot 相关的错误属性
 *
 * @author shenb
 * @date 2022-06-28 10:59
 */
@Component
public class MyCustomErrorAttributes extends DefaultErrorAttributes {

    private static final Logger logger = LoggerFactory.getLogger(MyCustomErrorAttributes.class);

    @Override
    public Map<String, Object> getErrorAttributes(
            WebRequest webRequest, ErrorAttributeOptions options) {
        Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, ErrorAttributeOptions.of(
                ErrorAttributeOptions.Include.MESSAGE,
                ErrorAttributeOptions.Include.EXCEPTION));

        String message = (String) errorAttributes.getOrDefault("message", errorAttributes.get("error"));
        Result<Void> result = Result.failure(message, null);
        return result.toMap();
    }
}